# 作者: spy
# 开发时间: 2021/12/24 22:34
import time

from selenium.webdriver.common.by import By
from base.base_page import BasePage
from pageobject.login_page import LoginPage
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class RTMessage(BasePage):
    """实时消息页面"""
    # 页面元素
    textarea_loc = (By.XPATH, "//textarea")  # 信息输入框
    send_loc = (By.XPATH, "//button")  # 发送按钮

    # 页面动作
    def send_message(self, user_index, message):  # 发送消息
        # 登录
        lp = LoginPage(self.driver)
        lp.login_TTOS("admin", "ant.design")
        # 点击拖车轨迹监控菜单项
        rt_time_button = self.driver.find_element(By.XPATH,
                                                  "//span[@class='ant-pro-menu-item-title'and text()='实时消息']")
        self.driver.execute_script('arguments[0].click();', rt_time_button)
        # 等待元素加载完成
        WebDriverWait(self.driver, 30).until(
            EC.visibility_of_element_located((By.XPATH, "//div[@class='rce-container-citem']")))
        # 实时消息
        user_list = self.driver.find_elements(By.XPATH, "//div[@class='rce-container-citem']")
        if len(user_list) > 0:
            user_list[user_index].click()
        else:
            print("没有用户")
        self.set_keys(RTMessage.textarea_loc, message)
        self.driver.implicitly_wait(3)
        self.click(RTMessage.send_loc)
